package com.engine.kq.cmd.dutyrules;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.kq.biz.KQGroupComInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import weaver.conn.RecordSet;
import weaver.filter.XssUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 值班规则--获取新建或编辑的表单
 */
public class GetDutyRulesFormCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetDutyRulesFormCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String ruleId = Util.null2String(params.get("ruleId"));//值班规则的ID
        boolean canAdd = HrmUserVarify.checkUserRight("KQDutyRulesAdd:Add", user);//是否具有新建权限
        boolean canEdit = HrmUserVarify.checkUserRight("KQDutyRulesEdit:Edit", user);//是否具有编辑的权限
        if (!canAdd && !canEdit) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
            return resultMap;
        }
        boolean isEdit = false;
        /*值班规则名称*/
        String name = "";
        /*考勤组的ID*/
        String groupIds = "";

        /**
         * 从这开始，下面的变量都是之前的写法，现在改成动态参数的，下面的这些参数就不用了
         */
        /*工作日是否允许值班：0-不允许、1-允许*/
        int overtimeEnable2 = 1;
        /*工作日的值班方式：1-需审批，以值班流程为准、2-需审批，以打卡为准，但是不能超过值班流程时长、3-无需审批，根据打卡时间计算值班时长*/
        int computingMode2 = 1;
        /*工作日下班多少分钟后开始计算值班*/
        int startTime2 = 30;
        /*工作日上班前多少分钟后开始计算值班*/
        int before_startTime2 = 0;
        /*工作日最小值班时长*/
        int minimumLen2 = 30;
        /*工作日是否允许值班转调休*/
        int paidLeaveEnable2 = 0;
        /*工作日值班转调休比例中的值班时长*/
        double lenOfDuty2 = 1;
        /*工作日值班转调休比例中的调休时长*/
        double lenOfLeave2 = 1;

        /*工作日值班补偿方式 1-默认值班补偿规则、2-根据员工在值班流程上选择的值班补偿类型进行补偿两种*/
        int paidLeaveEnableType2 = 0;
        /*工作日默认值班补偿方式 里转调休的方式: 1-按值班时长比例转调休时长、2-按值班时长范围设置转调休时长、3-按值班的时间段设置转调休时长*/
        int paidLeaveEnableDefaultType2 = 1;
        /*工作日 根据员工在值班流程上选择的值班补偿类型进行补偿 里转调休的方式: 1-按值班时长比例转调休时长、2-按值班时长范围设置转调休时长、3-按值班的时间段设置转调休时长*/
        int paidLeaveEnableFlowType2 = 1;

        //     按值班时长范围设置转调休时长: 值班时长 ≥* 分钟，转调休时长*分钟
        List<Map<String, String>> balanceLengthDetailLists2 = Lists.newArrayList();
        //      值班时间在19:00后，比例转调休时长 按照1:1
        List<Map<String, String>> balanceTimeDetailLists2 = Lists.newArrayList();
        /*工作日是否有休息时间*/
        int hasRestTime2 = 0;
        /*排除休息时间两种方式 :1、设置休息时间 2、根据值班时长扣除 */
        int restType2 = 1;
//     工作日 是否开启 自定义跨天值班归属分界点
        int has_cut_point2 = 0;
//     开启 自定义跨天值班归属分界点
        String cut_point2 = "00:00";


        /*休息日是否允许值班：0-不允许、1-允许*/
        int overtimeEnable3 = 1;
        /*休息日的值班方式：1-需审批，以值班流程为准、2-需审批，以打卡为准，但是不能超过值班流程时长、3-无需审批，根据打卡时间计算值班时长*/
        int computingMode3 = 1;
        /*休息日的最小值班时长*/
        int minimumLen3 = 30;
        /*休息日是否允许值班转调休*/
        int paidLeaveEnable3 = 0;
        /*休息日值班转调休比例中的值班时长*/
        double lenOfDuty3 = 1;
        /*休息日值班转调休比例中的调休时长*/
        double lenOfLeave3 = 1;
        /*休息日是否有休息时间*/
        int hasRestTime3 = 0;

        /*休息日值班补偿方式 1-默认值班补偿规则、2-根据员工在值班流程上选择的值班补偿类型进行补偿两种*/
        int paidLeaveEnableType3 = 0;
        /*休息日默认值班补偿方式 里转调休的方式: 1-按值班时长比例转调休时长、2-按值班时长范围设置转调休时长、3-按值班的时间段设置转调休时长*/
        int paidLeaveEnableDefaultType3 = 1;
        /*休息日 根据员工在值班流程上选择的值班补偿类型进行补偿 里转调休的方式: 1-按值班时长比例转调休时长、2-按值班时长范围设置转调休时长、3-按值班的时间段设置转调休时长*/
        int paidLeaveEnableFlowType3 = 1;

        //     按值班时长范围设置转调休时长: 值班时长 ≥* 分钟，转调休时长*分钟
        List<Map<String, String>> balanceLengthDetailLists3 = Lists.newArrayList();
        //      值班时间在19:00后，比例转调休时长 按照1:1
        List<Map<String, String>> balanceTimeDetailLists3 = Lists.newArrayList();
        /*排除休息时间两种方式 :1、设置休息时间 2、根据值班时长扣除 */
        int restType3 = 1;

        /*节假日是否允许值班：0-不允许、1-允许*/
        int overtimeEnable1 = 1;
        /*节假日的值班方式：1-需审批，以值班流程为准、2-需审批，以打卡为准，但是不能超过值班流程时长、3-无需审批，根据打卡时间计算值班时长*/
        int computingMode1 = 1;
        /*节假日的最小值班时长*/
        int minimumLen1 = 30;
        /*节假日是否允许值班转调休*/
        int paidLeaveEnable1 = 0;
        /*节假日值班转调休比例中的值班时长*/
        double lenOfDuty1 = 1;
        /*假节日值班转调休比例中的调休时长*/
        double lenOfLeave1 = 1;
        /*节假日是否有休息时间*/
        int hasRestTime1 = 0;

        /*节假日值班补偿方式 1-默认值班补偿规则、2-根据员工在值班流程上选择的值班补偿类型进行补偿两种*/
        int paidLeaveEnableType1 = 0;
        /*节假日默认值班补偿方式 里转调休的方式: 1-按值班时长比例转调休时长、2-按值班时长范围设置转调休时长、3-按值班的时间段设置转调休时长*/
        int paidLeaveEnableDefaultType1 = 1;
        /*节假日 根据员工在值班流程上选择的值班补偿类型进行补偿 里转调休的方式: 1-按值班时长比例转调休时长、2-按值班时长范围设置转调休时长、3-按值班的时间段设置转调休时长*/
        int paidLeaveEnableFlowType1 = 1;

        //     按值班时长范围设置转调休时长: 值班时长 ≥* 分钟，转调休时长*分钟
        List<Map<String, String>> balanceLengthDetailLists1 = Lists.newArrayList();
        //      值班时间在19:00后，比例转调休时长 按照1:1
        List<Map<String, String>> balanceTimeDetailLists1 = Lists.newArrayList();
        /*排除节假时间两种方式 :1、设置休息时间 2、根据值班时长扣除 */
        int restType1 = 1;
        /**
         * 到这里结束，这块区间的变量就用不到了，就起到一个文字说明的意义
         */

        //顺序显示，2是工作日，3是休息日，1是节假日
        int[] changetype = {2};

        Map<String, String> valueMap = Maps.newHashMap();
        if (!ruleId.equals("")) {
            String sql = "select t1.name,t1.groupids,t2.* from kq_DutyRules t1 left join kq_DutyRulesDetail t2 on t1.id=t2.ruleId where (t1.isDelete is null or t1.isDelete!=1) and t1.id=" + ruleId;
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(sql);
            int dayType = 0;//1-工作日、2-休息日、3-节假日
            while (recordSet.next()) {
                isEdit = true;
                name = recordSet.getString("name");
                groupIds = recordSet.getString("groupIds");
                dayType = recordSet.getInt("dayType");
                int detailid = recordSet.getInt("id");
                int dutyEnable = recordSet.getInt("dutyEnable");
                int computingMode = recordSet.getInt("computingMode");
                int startTime = recordSet.getInt("startTime");
                int before_startTime = recordSet.getInt("before_startTime");
                int minimumLen = recordSet.getInt("minimumLen");
                int paidLeaveEnable = recordSet.getInt("paidLeaveEnable");
                double lenOfDuty = recordSet.getDouble("lenOfDuty");
                double lenOfLeave = recordSet.getDouble("lenOfLeave");
                int hasRestTime = recordSet.getInt("hasRestTime");
                int has_cut_point = recordSet.getInt("has_cut_point");
                String cut_point = recordSet.getString("cut_point");
                if ("-1".equalsIgnoreCase(cut_point)) {
                    cut_point = "00:00";
                }
                if (has_cut_point < 0) {
                    has_cut_point = 0;
                }
                if (has_cut_point == 0) {
                    cut_point = "00:00";
                }
                if (before_startTime < 0) {
                    before_startTime = 0;
                }
                int paidLeaveEnableType = recordSet.getInt("paidLeaveEnableType");
                int paidLeaveEnableDefaultType = recordSet.getInt("paidLeaveEnableDefaultType");
                int paidLeaveEnableFlowType = recordSet.getInt("paidLeaveEnableFlowType");
                int restTimeType = recordSet.getInt("restTimeType");
                if (paidLeaveEnableType <= 0) {
                    paidLeaveEnableType = 1;
                }
                if (paidLeaveEnableDefaultType <= 0) {
                    paidLeaveEnableDefaultType = 1;
                }
                if (paidLeaveEnableFlowType <= 0) {
                    paidLeaveEnableFlowType = 1;
                }
                if (paidLeaveEnableDefaultType == 1 && paidLeaveEnable == 0) {
                    //如果是第一种生成调休方式，且没有开启关联调休，那么默认的值班转调休方式就是第一种
                    paidLeaveEnableDefaultType = 1;
                }
                if (restTimeType <= 0) {
                    restTimeType = 1;
                }

                valueMap.put("ID_" + dayType, detailid + "");

                valueMap.put("dutyEnable" + dayType, dutyEnable + "");
                valueMap.put("computingMode" + dayType, computingMode + "");
                valueMap.put("startTime" + dayType, startTime + "");
                valueMap.put("before_startTime" + dayType, before_startTime + "");
                valueMap.put("minimumLen" + dayType, minimumLen + "");
                valueMap.put("paidLeaveEnable" + dayType, paidLeaveEnable + "");
                valueMap.put("lenOfDuty" + dayType, lenOfDuty + "");
                valueMap.put("lenOfLeave" + dayType, lenOfLeave + "");
                valueMap.put("hasRestTime" + dayType, hasRestTime + "");
                valueMap.put("restTimeType" + dayType, restTimeType + "");
                valueMap.put("has_cut_point" + dayType, has_cut_point + "");
                valueMap.put("cut_point" + dayType, cut_point + "");
                valueMap.put("paidLeaveEnableType" + dayType, paidLeaveEnableType + "");
                valueMap.put("paidLeaveEnableDefaultType" + dayType, paidLeaveEnableDefaultType + "");
                valueMap.put("paidLeaveEnableFlowType" + dayType, paidLeaveEnableFlowType + "");
            }

            for (int i = 0; i < changetype.length; i++) {
                int changetype_index = changetype[i];
                String hasRestTime_index = "hasRestTime" + changetype_index;
                String paidLeaveEnable_index = "paidLeaveEnable" + changetype_index;
                String paidLeaveEnableType_index = "paidLeaveEnableType" + changetype_index;
                String paidLeaveEnableDefaultType_index = "paidLeaveEnableDefaultType" + changetype_index;
                String paidLeaveEnableFlowType_index = "paidLeaveEnableFlowType" + changetype_index;
                if (valueMap.containsKey(hasRestTime_index)) {
                    String hasRestTime = Util.null2String(valueMap.get(hasRestTime_index));
                    if ("1".equalsIgnoreCase(hasRestTime)) {
                        String restTimeType_index = "restTimeType" + changetype_index;
                        if (valueMap.containsKey(restTimeType_index)) {
                            String restTimeType = Util.null2String(valueMap.get(restTimeType_index));
                            if ("1".equalsIgnoreCase(restTimeType)) {
//                    排除休息时间
                                List restTimeList = new ArrayList();
                                sql = "select * from kq_DutyRestTime where ruleId=? and dayType=? order by dayType,id";
                                recordSet.executeQuery(sql, ruleId, changetype_index);
                                while (recordSet.next()) {
                                    String startType = recordSet.getString("startType");
                                    String startTime = recordSet.getString("startTime");
                                    String endType = recordSet.getString("endType");
                                    String endTime = recordSet.getString("endTime");

                                    Map<String, Object> restTimeMap = new HashMap<String, Object>();
                                    restTimeMap.put("startType", startType);
                                    restTimeMap.put("startTime", startTime);
                                    restTimeMap.put("endType", endType);
                                    restTimeMap.put("endTime", endTime);
                                    restTimeList.add(restTimeMap);
                                }
                                if (!restTimeList.isEmpty()) {
                                    resultMap.put("restTimeRule" + changetype_index, restTimeList);
                                }
                            } else if ("2".equalsIgnoreCase(restTimeType)) {
//                    排除休息时长
                                List restTimeList = new ArrayList();
                                sql = "select * from kq_DutyRestlength where ruleId=? and dayType=? order by dayType,dsporder";
                                recordSet.executeQuery(sql, ruleId, changetype_index);
                                while (recordSet.next()) {
                                    String overlength = recordSet.getString("overlength");
                                    String cutlength = recordSet.getString("cutlength");

                                    Map<String, Object> restTimeMap = new HashMap<String, Object>();
                                    restTimeMap.put("overtime", overlength);
                                    restTimeMap.put("exclude", cutlength);
                                    restTimeList.add(restTimeMap);
                                }
                                if (!restTimeList.isEmpty()) {
                                    resultMap.put("restTimeRule" + changetype_index, restTimeList);
                                }
                            }
                        }
                    }
                }
                if (valueMap.containsKey(paidLeaveEnableType_index)) {
                    String ID_index = "ID_" + changetype_index;
                    if (valueMap.containsKey(ID_index)) {
                        String detailid = Util.null2String(valueMap.get(ID_index));
                        String paidLeaveEnableType = Util.null2String(valueMap.get(paidLeaveEnableType_index));
                        String paidLeaveEnableDefaultType = Util.null2String(valueMap.get(paidLeaveEnableDefaultType_index));
                        String paidLeaveEnableFlowType = Util.null2String(valueMap.get(paidLeaveEnableFlowType_index));
                        if ("1".equalsIgnoreCase(paidLeaveEnableType)) {
                            if (valueMap.containsKey(paidLeaveEnable_index)) {
                                String paidLeaveEnable = Util.null2String(valueMap.get(paidLeaveEnable_index));
                                if ("1".equalsIgnoreCase(paidLeaveEnable)) {
                                    if ("2".equalsIgnoreCase(paidLeaveEnableDefaultType)) {
                                        set_lengthDetailList(detailid, changetype_index, resultMap);
                                    } else if ("3".equalsIgnoreCase(paidLeaveEnableDefaultType)) {
                                        set_TimeDetailList(detailid, changetype_index, resultMap);
                                    }
                                }
                            }
                        } else if ("2".equalsIgnoreCase(paidLeaveEnableType)) {
                            if ("2".equalsIgnoreCase(paidLeaveEnableFlowType)) {
                                set_lengthDetailList(detailid, changetype_index, resultMap);
                            } else if ("3".equalsIgnoreCase(paidLeaveEnableFlowType)) {
                                set_TimeDetailList(detailid, changetype_index, resultMap);
                            }
                        }
                    }
                }

            }
        }

        List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();
        Map<String, Object> groupItem = new HashMap<String, Object>();
        List<Object> itemList = new ArrayList<Object>();
        HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
        SearchConditionItem searchConditionItem = null;
        HrmFieldBean hrmFieldBean = null;

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("name");//值班规则名称
        hrmFieldBean.setFieldlabel("388831");
        hrmFieldBean.setFieldhtmltype("1");
        hrmFieldBean.setType("1");
        hrmFieldBean.setViewAttr(3);
        hrmFieldBean.setFieldvalue(name);
        hrmFieldBean.setIsFormField(true);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        searchConditionItem.setRules("required|string");
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("groupIds");//应用范围--考勤组
        hrmFieldBean.setFieldlabel("19374");
        hrmFieldBean.setFieldhtmltype("3");
        hrmFieldBean.setType("mkqgroup");
        hrmFieldBean.setFieldvalue(groupIds);
        hrmFieldBean.setIsFormField(true);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
        List<Map<String, Object>> replaceDatas = new ArrayList<Map<String, Object>>();
        Map<String, Object> datas = new HashMap<String, Object>();
        String[] groupIdArr = groupIds.split(",");
        for (String groupId : groupIdArr) {
            datas = new HashMap<String, Object>();
            datas.put("id", groupId);
            datas.put("name", kqGroupComInfo.getGroupname(groupId));
            replaceDatas.add(datas);
        }
        searchConditionItem.getBrowserConditionParam().setReplaceDatas(replaceDatas);
        /*一个考勤组只能关联一个值班规则，所以新建或者编辑的时候需要对考勤组浏览按钮内的数据进行过滤*/
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Map<String, Object> completeMap = new HashMap<String, Object>();
        String kqGroupIdStr = "";
        String sql = "select * from kq_DutyRules where (isDelete is null or isDelete !=1) ";
        if (isEdit) {
            sql += " and id !=" + ruleId;
        }
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            String kqGroupIds = recordSet.getString("groupIds");
            if (kqGroupIds.equals("")) {
                continue;
            }
            kqGroupIdStr += "," + kqGroupIds;
        }
        XssUtil xssUtil = new XssUtil();
        dataMap.put("sqlWhere", kqGroupIdStr.length() > 0 ? xssUtil.put(" kqType <>3 and id not in (" + kqGroupIdStr.substring(1) + ")") : xssUtil.put(" kqType <>3 "));
        completeMap.put("sqlWhere", kqGroupIdStr.length() > 0 ? xssUtil.put(" kqType <>3 and id not in (" + kqGroupIdStr.substring(1) + ")") : xssUtil.put(" kqType <>3 "));
        searchConditionItem.getBrowserConditionParam().setDataParams(dataMap);
        searchConditionItem.getBrowserConditionParam().setCompleteParams(completeMap);
        itemList.add(searchConditionItem);

        groupItem.put("title", SystemEnv.getHtmlLabelName(1361, user.getLanguage()));
        groupItem.put("defaultshow", true);
        groupItem.put("items", itemList);
        groupList.add(groupItem);

        for (int i = 0; i < changetype.length; i++) {
            int changetype_index = changetype[i];
            groupItem = new HashMap<String, Object>();
            itemList = new ArrayList<Object>();
            String overtimeEnable_key = "dutyEnable" + changetype_index;
            String overtimeEnable_value = isEdit ? Util.null2String(valueMap.get(overtimeEnable_key), "1") : "1";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(overtimeEnable_key);//是否允许值班
            hrmFieldBean.setFieldlabel("388833");
            hrmFieldBean.setFieldhtmltype("4");
            hrmFieldBean.setType("1");
            hrmFieldBean.setFieldvalue(overtimeEnable_value);
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setLabel("允许值班");
            Map<String, Object> otherParamsMap = new HashMap<String, Object>();
            otherParamsMap.put("display", "switch");
            searchConditionItem.setOtherParams(otherParamsMap);
            itemList.add(searchConditionItem);

            String paidLeaveEnableType_key = "paidLeaveEnableType" + changetype_index;
            String paidLeaveEnableType_value = isEdit ? Util.null2String(valueMap.get(paidLeaveEnableType_key), "1") : "1";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(paidLeaveEnableType_key);//值班补偿方式
            hrmFieldBean.setFieldlabel("388838");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            List<SearchConditionOption> paidLeaveEnableOptions = new ArrayList<SearchConditionOption>();
            paidLeaveEnableOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(524828, user.getLanguage()), "1".equalsIgnoreCase(paidLeaveEnableType_value)));
            paidLeaveEnableOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(524833, user.getLanguage()), "2".equalsIgnoreCase(paidLeaveEnableType_value)));
            searchConditionItem.setOptions(paidLeaveEnableOptions);
            searchConditionItem.setDetailtype(3);
            itemList.add(searchConditionItem);

            String paidLeaveEnableDefaultType_key = "paidLeaveEnableDefaultType" + changetype_index;
            String paidLeaveEnableDefaultType_value = isEdit ? Util.null2String(valueMap.get(paidLeaveEnableDefaultType_key), "1") : "1";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(paidLeaveEnableDefaultType_key);//默认值班补偿方式 里转调休的方式
            hrmFieldBean.setFieldlabel("524828");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("2");
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            List<SearchConditionOption> paidLeaveDefaultTypeOptions = new ArrayList<SearchConditionOption>();
            paidLeaveDefaultTypeOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(524830, user.getLanguage()), "1".equalsIgnoreCase(paidLeaveEnableDefaultType_value)));
            paidLeaveDefaultTypeOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(524831, user.getLanguage()), "2".equalsIgnoreCase(paidLeaveEnableDefaultType_value)));
//          这个方式只是临时屏蔽，后面可能还是会启用，不要占用这个key是3的
//          paidLeaveDefaultTypeOptions.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(524832, user.getLanguage()),"3".equalsIgnoreCase(paidLeaveEnableDefaultType_value)));
            searchConditionItem.setOptions(paidLeaveDefaultTypeOptions);
            searchConditionItem.setDetailtype(1);
            itemList.add(searchConditionItem);

            String paidLeaveEnableFlowType_key = "paidLeaveEnableFlowType" + changetype_index;
            String paidLeaveEnableFlowType_value = isEdit ? Util.null2String(valueMap.get(paidLeaveEnableFlowType_key), "1") : "1";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(paidLeaveEnableFlowType_key);//根据员工在值班流程上选择的值班补偿类型进行补偿
            hrmFieldBean.setFieldlabel("524833");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("2");
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            List<SearchConditionOption> paidLeaveFlowTypeOptions = new ArrayList<SearchConditionOption>();
            paidLeaveFlowTypeOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(524830, user.getLanguage()), "1".equalsIgnoreCase(paidLeaveEnableFlowType_value)));
            paidLeaveFlowTypeOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(524831, user.getLanguage()), "2".equalsIgnoreCase(paidLeaveEnableFlowType_value)));
//          这个方式只是临时屏蔽，后面可能还是会启用，不要占用这个key是3的
//          paidLeaveFlowTypeOptions.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(524832, user.getLanguage()),"3".equalsIgnoreCase(paidLeaveEnableFlowType_value)));
            searchConditionItem.setOptions(paidLeaveFlowTypeOptions);
            searchConditionItem.setDetailtype(1);
            itemList.add(searchConditionItem);

            String paidLeaveEnable_key = "paidLeaveEnable" + changetype_index;
            String paidLeaveEnable_value = isEdit ? Util.null2String(valueMap.get(paidLeaveEnable_key), "0") : "0";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(paidLeaveEnable_key);//是否允许值班转调休
            hrmFieldBean.setFieldlabel("388838");
            hrmFieldBean.setFieldhtmltype("4");
            hrmFieldBean.setType("1");
            hrmFieldBean.setFieldvalue(paidLeaveEnable_value);
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            itemList.add(searchConditionItem);

            String lenOfDuty_key = "lenOfDuty" + changetype_index;
            String lenOfDuty_value = isEdit ? Util.null2String(valueMap.get(lenOfDuty_key), "1") : "1";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(lenOfDuty_key);//值班转调休比例中的值班时长
            hrmFieldBean.setFieldlabel("389222");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setViewAttr(3);
            hrmFieldBean.setFieldvalue(lenOfDuty_value);
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|numeric");
            searchConditionItem.setStep("0.01");
            searchConditionItem.setPrecision(2);
            itemList.add(searchConditionItem);

            String lenOfLeave_key = "lenOfLeave" + changetype_index;
            String lenOfLeave_value = isEdit ? Util.null2String(valueMap.get(lenOfLeave_key), "1") : "1";
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(lenOfLeave_key);//值班转调休比例中的调休时长
            hrmFieldBean.setFieldlabel("389223");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setViewAttr(3);
            hrmFieldBean.setFieldvalue(lenOfLeave_value);
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|numeric");
            searchConditionItem.setStep("0.01");
            searchConditionItem.setPrecision(2);
            itemList.add(searchConditionItem);


//          2是工作日，3是休息日，1是节假日
            if (changetype_index == 2) {
                groupItem.put("title", SystemEnv.getHtmlLabelName(388841, user.getLanguage()));//工作日值班
            } else if (changetype_index == 3) {
                groupItem.put("title", SystemEnv.getHtmlLabelName(389730, user.getLanguage()));//休息日值班
            } else if (changetype_index == 1) {
                groupItem.put("title", SystemEnv.getHtmlLabelName(1959, user.getLanguage()));//节假日值班
            }
            groupItem.put("defaultshow", true);
            groupItem.put("items", itemList);
            groupList.add(groupItem);
        }
        resultMap.put("condition", groupList);

        /**
         * 判断调休是否已设置
         */
        sql = "select * from kq_LeaveRules where (isDelete is null or isDelete <>1) and id in (select ruleId from kq_LeaveRulesDetail where (isDelete is null or isDelete <>1) and distributionMode=5)";
        recordSet.executeQuery(sql);
        boolean hasTXRules = recordSet.getCounts() > 0;
        resultMap.put("hasTXrules", hasTXRules);
        return resultMap;
    }

    private void set_TimeDetailList(String detailid, int changetype_index, Map<String, Object> resultMap) {
        RecordSet recordSet = new RecordSet();
        List timeDetailList = new ArrayList();
        String sql = "select * from kq_DutyBalanceTimeDetail where rulesdetailid=? order by timepoint";
        recordSet.executeQuery(sql, detailid);
        while (recordSet.next()) {
            String timepoint = recordSet.getString("timepoint");
            String lenOfDuty = recordSet.getString("lenOfDuty");
            String lenOfLeave = recordSet.getString("lenOfLeave");
            String dsporder = recordSet.getString("dsporder");

            Map<String, Object> timeDetailMap = new HashMap<String, Object>();
            timeDetailMap.put("time", timepoint);
            timeDetailMap.put("left", lenOfDuty);
            timeDetailMap.put("right", lenOfLeave);
            timeDetailMap.put("dsporder", dsporder);
            timeDetailList.add(timeDetailMap);
        }
        if (!timeDetailList.isEmpty()) {
            resultMap.put("paidLeaveEnableTypeRule" + changetype_index, timeDetailList);
        }
    }

    private void set_lengthDetailList(String detailid, int changetype_index,
                                      Map<String, Object> resultMap) {

        RecordSet recordSet = new RecordSet();
        List lengthDetailList = new ArrayList();
        String sql = "select * from kq_DutyBalanceLengthDetail where rulesdetailid=? order by overtimelength";
        recordSet.executeQuery(sql, detailid);
        while (recordSet.next()) {
            String overtimelength = recordSet.getString("overtimelength");
            String balancelength = recordSet.getString("balancelength");
            String dsporder = recordSet.getString("dsporder");

            Map<String, Object> lengthDetailMap = new HashMap<String, Object>();
            lengthDetailMap.put("work", overtimelength);
            lengthDetailMap.put("rest", balancelength);
            lengthDetailMap.put("dsporder", dsporder);
            lengthDetailList.add(lengthDetailMap);
        }
        if (!lengthDetailList.isEmpty()) {
            resultMap.put("paidLeaveEnableTypeRule" + changetype_index, lengthDetailList);
        }
    }
}
